package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeginClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PatientServiceImpl
        extends ServiceImpl<PatientMapper,Patient>implements PatientService{

    @Autowired
    private DictFeginClient dictFeginClient;

    // 根据用户id获取就诊列表
    @Override
    public List<Patient> findAllUserId(Long userId) {

        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);

        // 通过远程调用,得到编码相应的内容,查询数据字典得到确定的值 (service_cmn_client的作用)
        // 通过流的方式将patientList集合遍历得到每个patient对象,
        // 然后向每个patient对象中通过params属性设立其他值,方便以后显示
        patientList.stream().forEach(item ->{

            // 其他参数的封装
            this.packPatient(item);
        });
        return patientList;
    }

    @Override
    public Patient getPatientById(Long id) {

        return this.packPatient(baseMapper.selectById(id));
    }

    private Patient packPatient(Patient patient) {

        // 获取证件类型
        String certificatesTypeString =
                dictFeginClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        // 联系人证件类型
        String contactsCertificatesTypeString =
                dictFeginClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());

        // 获取省的名称
        String provinceString = dictFeginClient.getName(patient.getProvinceCode());
        String cityString = dictFeginClient.getName(patient.getCityCode());
        String districtString = dictFeginClient.getName(patient.getDistrictCode());

        patient.getParam().put("certificatesTypeString",certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString",contactsCertificatesTypeString);
        patient.getParam().put("provinceString",provinceString);
        patient.getParam().put("cityString",cityString);
        patient.getParam().put("districtString",districtString);
        patient.getParam().put("fullAddress",provinceString + cityString + districtString + patient.getAddress());

        return patient;

    }
}
